package com.ruoyi.web.controller.shop.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.VO.product.ProductCategorySearchVO;
import com.ruoyi.common.entity.VO.shop.ShopChangeParentVO;
import com.ruoyi.common.entity.VO.shop.ShopSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.product.service.IBackProductCategoryService;
import com.ruoyi.web.controller.shop.service.IBackShopService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店铺管理
 */
@Controller
@RequestMapping("/back/shop")
public class BackShopController extends BaseController {
    private String prefix = "shop";

    @Autowired
    private IBackShopService shopService;

    @Autowired
    private IBackProductCategoryService productCategoryService;

    /**
     * 页面-店铺列表
     *
     * @return
     */
    @RequiresPermissions("back:shop:list")
    @GetMapping
    public String list() {
        return prefix + "/list";
    }

    /**
     * 页面-开店申请列表
     *
     * @return
     */
    @RequiresPermissions("back:shopApply:list")
    @GetMapping("/applyList")
    public String applyList() {
        return prefix + "/applyList";
    }

    /**
     * 页面-新增店铺
     *
     * @return
     */
    @RequiresPermissions("back:shopApply:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 页面-编辑店铺
     *
     * @param shopId 店铺ID
     * @return
     */
    @RequiresPermissions("back:shop:view")
    @GetMapping("/edit/{shopId}")
    public String edit(@PathVariable("shopId") Long shopId, ModelMap mmap) {
        ProductCategorySearchVO productCategorySearchVO = new ProductCategorySearchVO();
        productCategorySearchVO.setParentId(0L);
        productCategorySearchVO.setIsEnable(1);
        productCategorySearchVO.setIsDelete(0);
        mmap.put("categoryList", productCategoryService.searchCategoryList(productCategorySearchVO));
        mmap.put("shop", shopService.getShopInfoById(shopId));
        return prefix + "/edit";
    }

    /**
     * 页面-店铺-编辑店铺基本信息
     *
     * @return
     */
    @RequiresPermissions("back:shop:info")
    @GetMapping("/info")
    public String info(ModelMap mmap) {
        if(0 == ShiroUtils.getShopId()){
            return "redirect:/back/shop/switchShop";
        }
        ProductCategorySearchVO productCategorySearchVO = new ProductCategorySearchVO();
        productCategorySearchVO.setParentId(0L);
        productCategorySearchVO.setIsEnable(1);
        productCategorySearchVO.setIsDelete(0);
        mmap.put("categoryList", productCategoryService.searchCategoryList(productCategorySearchVO));
        mmap.put("shop", shopService.getShopInfoById(ShiroUtils.getShopId()));
        return prefix + "/info";
    }

    /**
     * 页面-查看店铺信息
     */
    @RequiresPermissions("back:shop:detail")
    @GetMapping("/detail/{shopId}")
    public String detail(@PathVariable("shopId") Long shopId, ModelMap mmap) {
        return prefix + "/detail";
    }

    /**
     * 页面-修改店铺上级
     */
    @RequiresPermissions("back:shop:edit")
    @GetMapping("/changeParent/{shopId}")
    public String changeParent(@PathVariable("shopId") Long shopId, ModelMap mmap) {
        ShopVO shopInfo = shopService.getShopInfoById(shopId);
        if (StringUtils.isNotNull(shopInfo) && 0L == shopInfo.getParentId()) {
            shopInfo.setParentShopName("无");
        }
        mmap.put("shopInfo", shopInfo);
        return prefix + "/changeParent";
    }


    /**
     * 选择店铺树
     */
    @GetMapping("/selectShopTree/{shopId}")
    public String selectShopTree(@PathVariable("shopId") Long shopId, ModelMap mmap) {
        mmap.put("shop", shopService.getShopInfoById(shopId));
        return prefix + "/tree";
    }


    /**
     * 店铺切换-数据处理
     *
     * @return
     */
    @GetMapping("/switchShop")
    public String switchShop(ModelMap mmap) {
        mmap.put("shopId", ShiroUtils.getShopId());
        ShopSearchVO shopSearchVO = new ShopSearchVO();
        shopSearchVO.setIsDelete(0);
        shopSearchVO.setReviewStatus(1);
        List<ShopVO> shopList = shopService.searchList(shopSearchVO);
        ShopVO shopVO = new ShopVO();
        shopVO.setId(0L);
        shopVO.setShopName("切换站点");
        shopList.add(shopVO);
        mmap.put("shopList", shopList);
        return prefix+"/switchShop";
    }

    /**
     * 店铺切换-数据处理
     *
     * @param shopId
     * @return
     */
    @RequiresPermissions("back:shop:switch")
    @PostMapping("/switch")
    @ResponseBody
    public AjaxResult switchShop(Long shopId) {
        ShiroUtils.switchShop(shopId);
        return success();
    }

    /**
     * 数据查询-店铺列表（搜索）
     *
     * @param shopSearchVO
     * @return
     */
    @RequiresPermissions("back:shop:list")
    @PostMapping("/searchList")
    @ResponseBody
    public TableDataInfo searchList(ShopSearchVO shopSearchVO) {
        startPage();
        List<ShopVO> list = shopService.searchList(shopSearchVO);
        return getDataTable(list);
    }

    /**
     * 数据查询-校验店铺名称
     */
    @PostMapping("/checkShopNameUnique")
    @ResponseBody
    public String checkShopNameUnique(ShopSearchVO shopSearchVO) {
        return shopService.checkShopNameUnique(shopSearchVO);
    }

    /**
     * 数据查询-校验店铺昵称
     */
    @PostMapping("/checkShopCodeUnique")
    @ResponseBody
    public String checkShopCodeUnique(ShopSearchVO shopSearchVO) {
        return shopService.checkShopCodeUnique(shopSearchVO);
    }

    /**
     * 数据新增-添加店铺
     *
     * @param shopVO
     * @return
     */
    @Log(title = "店铺管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:shopApply:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(ShopVO shopVO) {
        ShopSearchVO shopSearchVO = new ShopSearchVO();
        shopSearchVO.setShopName(shopVO.getShopName());
        if (Constants.FAIL.equals(shopService.checkShopNameUnique(shopSearchVO))) {
            return error("新增店铺'" + shopVO.getShopName() + "'失败，店铺已存在不允许覆盖");
        }
        shopVO.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        return toAjax(shopService.insertShop(shopVO));
    }

    /**
     * 数据更新-编辑店铺信息
     *
     * @param shopVO 店铺信息
     * @return
     */
    @Log(title = "店铺管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions(value = {"back:shop:edit", "back:shop:editMyself"}, logical = Logical.OR)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopVO shopVO) {
        ShopSearchVO shopSearchVO = new ShopSearchVO();
        if (ShiroUtils.getShopId() == 0) {
            shopSearchVO.setShopId(shopVO.getId());
        } else {
            if (ShiroUtils.getSysUser().isSwitch() && shopVO.getId() != 0) {
                shopSearchVO.setShopId(shopVO.getId());
            } else {
                shopSearchVO.setShopId(ShiroUtils.getShopId());
            }
            shopVO.setId(shopSearchVO.getShopId());
        }
        shopSearchVO.setShopName(shopVO.getShopName());
        if (Constants.FAIL.equals(shopService.checkShopNameUnique(shopSearchVO))) {
            return error("修改店铺'" + shopVO.getShopName() + "'失败，店铺已存在不允许覆盖");
        }
        shopVO.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        return toAjax(shopService.updateShop(shopVO));
    }


    /**
     * 数据更新-删除店铺
     *
     * @param ids
     * @return
     */
    @Log(title = "店铺管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("back:shop:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult delete(String ids) {
        Long shopId = Long.valueOf(ids);
        return toAjax(shopService.deleteShopById(shopId));
    }

    /**
     * 数据更新-修改上级
     *
     * @param shopChangeParentVO
     * @return
     */
    @Log(title = "店铺管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:shop:edit")
    @PostMapping("/changeParent")
    @ResponseBody
    public AjaxResult changeParent(ShopChangeParentVO shopChangeParentVO) {
//        backShopBalanceRecordService.checkFinancePassword(shopChangeParentVO.getPassword());
        shopService.changeParentHandle(shopChangeParentVO);
        return success();
    }

    /**
     * 加载店铺列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = shopService.selectShopTree(new ShopSearchVO());
        return ztrees;
    }
}
